<?php

class ThemeController extends CoreController {

	function __construct()
	{
		parent::__construct();
		$this->init();
	}

	public function init()
	{
		add_action('wp_enqueue_scripts',array($this,'frontEnd'));
		add_action('action_facebook',array($this,'actionFaceBook'));
		add_action('action_header',array($this,'header'));
		add_action('action_footer',array($this,'footer'));
		add_action('action_sidebar',array($this,'sidebar'));
		add_action('action_search',array($this,'search'));
		add_action('action_index',array($this,'index'));
		add_action('action_single',array($this,'single'));
		add_action('action_category',array($this,'category'));
		add_action('action_menu',array($this,'menu'));
		add_action('action_featured',array($this,'featured'));
		add_action('action_googleSearch',array($this,'googleSearch'));
		add_action('action_page',array($this,'page'));
		add_action('action_topPracticeTest',array($this,'topPracticeTest'));
		add_action('action_author',array($this,'author'));
		add_theme_support('menus');
		add_theme_support('post-thumbnails');
	}

	public function index()
	{
		$this->loadModel(array('ArticleModel','CategoryModel'));
		$this->header();
		$this->render('index',array('posts'=>$this->ArticleModel->home()));
		$this->footer();
	}

	public function googleSearch()
	{
		$this->header();
		$this->render('googleSearch');
		$this->footer();
	}

	public function header()
	{
		$this->loadComponent(array('facebookComponent'));
		$this->loadModel(array('FaceBookLoginModel','PracticeTestResultModel'));
		$userID = $this->facebookComponent->getUserID();
		$userProfile = $this->FaceBookLoginModel->getUserProfile($userID);
		$this->render('header',array('userID'=>$userID,'userProfile'=>$userProfile));
	}

	public function page()
	{
		$this->header();
		$this->render('page');
		$this->footer();
	}

	public function topPracticeTest()
	{
		$this->loadModel(array('FaceBookLoginModel'));
		$this->header();
		$topUsers = $this->FaceBookLoginModel->topUserPracticeTest();
		if(count($topUsers)<3){
			wp_redirect(home_url());exit;
		}
		$this->render('top-quiz',['topUsers'=>$topUsers]);
		$this->footer();
	}

	public function footer()
	{
		$this->render('footer');
	}

	public function isViewBanner()
	{
		if(is_single()){
			return false;
		}else{
			if(is_page()){
				if(is_page('search')){
					return true;
				}else{
					return false;
				}
			}else{
				return true;
			}
		}
	}

	public function isViewSearch()
	{
		global $post;
		if(is_single()){
			$post_meta = get_post_meta($post->ID);
			if($post_meta['meta-question'][0]=="YES" && strlen($post_meta['meta-youtube'][0])>6){
				return true;
			}else{
				return false;
			}
		}else{
			if(is_page()){
				if(is_page('search')){
					return true;
				}else{
					return false;
				}
			}else{
				return true;
			}
		}
	}

	public function single()
	{
		global $post;
		$this->loadModel(array('ArticleModel','FaceBookLoginModel','PracticeTestModel','PracticeTestAnswerModel','PracticeTestQuestionModel','PracticeTestTemporaryResultModel','PracticeTestResultModel','CategoryModel'));
		$this->loadComponent('facebookComponent');
		$post_meta = get_post_meta($post->ID);
		$isSingle = ($post_meta['meta-question'][0]=="YES" && strlen($post_meta['meta-youtube'][0])>6) ? "single" : "single-post";
		$postSingle = $this->ArticleModel->getPost($post->ID);
		$userID = $this->facebookComponent->getUserID();
		$userProfile = $this->FaceBookLoginModel->getUserProfile($userID);
		$practiceTest = $this->PracticeTestModel->getPracticeTest($post->ID);
		$question = $this->PracticeTestQuestionModel->getPracticeTestQuestion($practiceTest->id);
		$currentQuestion = $this->PracticeTestTemporaryResultModel->getCurrentQuestion($practiceTest->id,$userID);
		$nextTimeDoing = $this->PracticeTestTemporaryResultModel->getNextDoing($practiceTest->id,$userID);
		$numberCorrect = $this->PracticeTestTemporaryResultModel->getNumberAnswerCorrect($practiceTest->id,$userID);
		$getPoint = $this->PracticeTestTemporaryResultModel->getPoints($practiceTest->id,$userID);
		$resultExist = $this->PracticeTestResultModel->exists($practiceTest->id,$userID);
		$resultTMPExist = $this->PracticeTestTemporaryResultModel->isExists($practiceTest->id,$userID);
		$timerDoing = $this->PracticeTestTemporaryResultModel->getTimerDoing($practiceTest->id,$userID);
		$timerDoing = date('Y-m-d H:i:s',strtotime($timerDoing."+ 7 hours"));
		$isSuccess = $this->PracticeTestTemporaryResultModel->isSuccess($practiceTest->id,$userID);
		$isUserReview = get_post_meta($post->ID,$userID,true);
		$seriesPost = $this->ArticleModel->seriesPost($post->ID);
		if(is_single()){
			$this->registerScript('practicetest_js',CORE_JS_URL.'practicetest/jquery.practicetest.js',array('jquery'));
			$this->registerScript('practicetest_ajax',CORE_JS_URL.'practicetest/ajax.practicetest.js',array('jquery'));
			$this->registerScript('jquery-countdown',CORE_JS_URL.'jquery.countdown.js',array('jquery'));
			$this->registerScript('jquery-screwdefaultbuttons',CORE_THEME_JS.'jquery.screwdefaultbuttonsV2.min.js',array('jquery'));
			
			wp_localize_script('practicetest_js','core_js_script', array(
	            'ajax_image' => CORE_IMAGE_URL,
	            'ajaxurl' => admin_url('admin-ajax.php'),
	            'timerDoing'=>$timerDoing
	        ));
	        wp_localize_script('practicetest_ajax','core_ajax_script', array(
	            'ajax_image' => CORE_IMAGE_URL,
	            'ajaxurl' => admin_url('admin-ajax.php'),
	            'userID'=>$userID,
	            'testID'=>$practiceTest->id
	        ));
		}

		$this->header();
		$this->render($isSingle,array('post'=>$postSingle,'seriesPost'=>$seriesPost,'numberCorrect'=>$numberCorrect,'FBUser'=>$userProfile,'practiceTest'=>$practiceTest,'question'=>$question,'currentQuestion'=>$currentQuestion,'nextTimeDoing'=>$nextTimeDoing,'isSuccess'=>$isSuccess,'getPoint'=>$getPoint,'resultExist'=>$resultExist,'resultTMPExist'=>$resultTMPExist,'post_meta'=>$post_meta,'isUserReview'=>$isUserReview));
		$this->footer();
	}

	public function category()
	{
		$this->loadModel(array('CategoryModel'));
		$category_id = get_cat_ID(single_term_title("", false));
		$this->header();
		$this->render('category',array('postCount'=>$this->CategoryModel->countPostByCategory(single_cat_title('',false)),'category_id'=>$category_id));
		$this->footer();
	}

	public function sidebar()
	{
		$this->render('sidebar');
	}

	public function author()
	{
		$this->header();
		$this->render('author');
		$this->footer();
	}

	public function sidebarSecond()
	{
		global $post;
		$this->loadModel('ArticleModel');
		$this->render('sidebar-second',array('seriesPost'=>$this->ArticleModel->seriesPost($post->ID)));
	}

	public function search()
	{
		$this->render('search');
	}

	public function menu()
	{
		$this->render('menu');
	}

	public function featured()
	{
		$this->loadModel(array('ArticleModel'));
		$this->render('featured',array('posts'=>$this->ArticleModel->featured(),'isView'=>'featured'));
	}

	public function featuredHosting()
	{
		$this->loadModel(array('ArticleModel'));
		$this->render('featured',array('posts'=>$this->ArticleModel->hostingReview(),'isView'=>'featuredHosting'));
	}

	public function frontEnd()
	{
		wp_enqueue_style('iztheme-style', CORE_THEME_CSS."style.min.css");
		$this->registerScript('jquery-waypoint',CORE_THEME_JS.'waypoints.min.js',array('jquery'));
		$this->registerScript('jquery-mousewheel',CORE_THEME_JS.'jquery.mousewheel.js',array('jquery'));
		$this->registerScript('jquery-nicescroll',CORE_THEME_JS.'jquery.nicescroll.min.js',array('jquery'));
		$this->registerScript('jquery-swfobject',CORE_THEME_JS.'swfobject.js',array('jquery'));
       	$this->registerScript('jquery-nprogress',CORE_THEME_JS.'nprogress.js',array('jquery'));
		$this->registerScript('jquery-application',CORE_THEME_JS.'application.js',array('jquery'),true,true);
		wp_localize_script('jquery-application','theme_js_script', array(
            'ajax_image' => CORE_THEME_IMAGE,
            'ajaxurl' => admin_url('admin-ajax.php'),
        ));
        $this->registerScript('jquery-youtube',CORE_THEME_JS.'youtube.player.js',array('jquery'),true,true);
        // $this->registerScript('jquery-flymenu',CORE_THEME_JS.'flyMenu.js',array('jquery'),true,true);
	}

	public function actionFaceBook()
	{
		new FaceBookLoginController();
	}

	public function actionPracticeTest()
	{
		new PracticeTestController();
	}

}